﻿using System;
using System.Reflection;
using System.Web.UI;

namespace HeroldIT.Dnn.Modules.WebControls
{
    /// <summary>
    /// A control showing apollo's ModuleTitleLocalization control if available.
    /// </summary>
    public class LocalizedModuleTitleControl : UserControl
    {
        /// <summary>
        /// Holds the ModuleTitleLocalization control.
        /// </summary>
        private Control ucLocalizedModuleTitle;

        /// <summary>
        /// Occurs when the control is initialized.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs"/> passed to the Init event.</param>
        protected override void OnInit(EventArgs e)
        {
            if (Localizer.ModuleLocalizationAvailable)
            {
                this.Visible = true;

                this.ucLocalizedModuleTitle =
                    this.LoadControl("~/DesktopModules/ModuleLocalization/ModuleTitleLocalization.ascx");
                this.Controls.Add(this.ucLocalizedModuleTitle);
            }
            else
            {
                this.Visible = false;
            }
            base.OnInit(e);
        }

        /// <summary>
        /// Stores the localized module title.
        /// </summary>
        public void Update()
        {
            if (null != this.ucLocalizedModuleTitle)
            {
                MethodInfo method = this.ucLocalizedModuleTitle.GetType().GetMethod("update", Type.EmptyTypes);
                if (null == method)
                {
                    return;
                }
                method.Invoke(this.ucLocalizedModuleTitle, new object[0]);
            }
        }
    }
}
